


package com.cy.module;

import com.alibaba.fastjson.JSONObject;
import com.cy.config.EnumConst;
import com.cy.ext.plugin.route.ControllerBind;
import com.cy.model.Account;
import com.cy.model.User;
import com.jfinal.plugin.activerecord.Page;
import com.cy.model.ServiceMessage;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Dio-Nbook on 7-8.
 */
@ControllerBind(controllerKey = "/clmp/serviceMessage")
public class ServiceMessageController extends WebApiController {
    @Override
    public void index() {
        ServiceMessage serviceMessage = getModel(ServiceMessage.class);
        int page = getParaToInt("pageIndex");
        int pageSize = getParaToInt("limit");
        Map<String, String[]> parasMap = getParaMap();
        if (null != parasMap){
            String start = null ;
            if(null != parasMap.get("start_time")){
                start = parasMap.get("start_time")[0];
            }
            String end = null;
            if (null != parasMap.get("end_time")){
                end = parasMap.get("end_time")[0];
            }
            if (StringUtils.isNotBlank(start)){
                serviceMessage.put("start",start);
            }
            if (StringUtils.isNotBlank(end)){
                serviceMessage.put("end",end);
            }

            String mobile = null;
            if (null != parasMap.get("mobile")){
                mobile = parasMap.get("mobile")[0];
            }
            if (StringUtils.isNotBlank(mobile)){
                serviceMessage.put("mobile",mobile);
            }
        }
        String select = "sm.`id`,sm.`status`,sm.`user_id`,`message`,sm.`type`,sm.`create_time`,`account_id`,`reply`,`reply_time`,u.mobile ";
        Page<ServiceMessage> serviceMessagePage = ServiceMessage.dao.paginate(page, pageSize, select, serviceMessage);
        renderBuiPage(serviceMessagePage, 200);
    }

    @Override
    public void show() {

    }

   @Override
    public void update() {
        if(null == getLoginAccount()){
            renderRetCode(EnumConst.RetCode.NOT_SIGNIN);
            return;
        }
        int id = getParaToInt();
        if(id < 1){
            renderRetCode(EnumConst.RetCode.PARAMETER_IS_INVALID);
            return;
        }
        ServiceMessage tempServiceMessage = getModel(ServiceMessage.class);
        ServiceMessage serviceMessage = ServiceMessage.dao.findById(id);
        if(null == serviceMessage){
            renderRetCode(EnumConst.RetCode.PARAMETER_IS_INVALID);
            return;
        }
        serviceMessage.cover(tempServiceMessage);
        serviceMessage.setAccountId(getLoginAccount().getAccountId()).setReplyTime(new Date()).setStatus(1);

        if (serviceMessage.update()) {
            renderSuccess();
        } else {
            renderFail();
        }
    }

    @Override
    public void save() {
        if(null == getLoginUser()){
            renderRetCode(EnumConst.RetCode.NOT_SIGNIN);
            return;
        }
        Map<String,Object> map=getRequestBody();
        if(null == map.get("message") || null == map.get("type")){
            renderRetCode(EnumConst.RetCode.PARAMETER_IS_NULL);
            return;
        }
        ServiceMessage serviceMessage = new ServiceMessage();
        serviceMessage.setUserId(getLoginUser().getUserId()).setMessage(map.get("message").toString()).setType((Integer) map.get("type")).setStatus(0).setIsRead(1).setCreateTime(new Date());

        if (serviceMessage.save()) {
            renderSuccess();
        } else {
            renderFail();
        }
    }

    @Override
    public void delete() {
        String[] ids = getPara().split("-");
        for (String id : ids) {
            ServiceMessage.dao.deleteById(id);
        }
        renderSuccess();
    }

    public void fatchMsg(){
        User user = getLoginUser();
        if(null == user){
            renderRetCode(EnumConst.RetCode.NOT_SIGNIN);
            return;
        }
        JSONObject json = new JSONObject();
        List<ServiceMessage> smList = ServiceMessage.dao.findUserMsg(user.getUserId());
        json.put("status",EnumConst.RetCode.SUCCESS.getCode());
        json.put("msgs",smList);
        renderJson(json);

    }
}
